<script>
function LinkedList() {
    let Node = function (element) {
        this.element = element;
        this.next = null
    }

    // 为什么不使用this.head？？
    var head = null
    var length = 0

    // 向尾部 添加 元素
    LinkedList.prototype.append = function (element) {
    
        var node = new Node(element),
            current;
        
        if (head===null) {
            head = node
        }else {
            current = head

            while(current.next) {
                current = current.next
            }
            // 找到最后一项，将其next赋为element
            current.next = node
        }
        length++
    }

    // 从链表中 移除 元素
    LinkedList.prototype.remove = function (position) {
        // 为什么要使用position 删除的时候还要事先获得位置？？
        if (position > -1 && position < length) {
            var current = head,
                previous,
                index = 0;
            if (position === 0) {
                head = current.next
            }else {
                while (index++ < position) {
                    previous = current
                    current = current.next
                }

                previous.next = current.next
            }

            length --

            return current.element
        }else {
            return null;
        }
    }

    //在任意位置 插入 元素
    LinkedList.prototype.insert = function (position, element) {
        if (position >= 0 && position <= length){
            var node = new Node(element),
                current = head,
                previous,
                index = 0;
            if (position === 0) {
                node.next = current;
                head = node
            }else {
                while (index++ < position) {
                    previous = current
                    current = current.next
                }
                node.next = current;
                previous.next = node
            }
            length++
            return true
        }else {
            return false
        }
        
        
    }

    // 返回元素的位置
    LinkedList.prototype.indexOf = function (element) {
        var current = head,
            index = -1;

        if (head) {
            while (current) {
                if (current.element === element) {
                    return index
                }
                index++
                current = current.next
            }
        }

        return -1
    }

    // isEmpty 
    LinkedList.prototype.isEmpty = function () {
        return length === 0
    }

    // resize
    LinkedList.prototype.size = function () {
        return length
    }

    // getHead
    LinkedList.prototype.getHead = function () {
        return head
    }
    // 将 LinkedList 转换成一个字符串
    LinkedList.prototype.toString = function () {
        var current = head,
            string = '';
        
            while (current) {
                string += current.element + "->"
                current = current.next
            }

            return string
    }







}

let list = new LinkedList()
list.append(15)
list.append(10)

console.log(list);

</script>